import requests
import re
import time

from lxml import etree
for i in range(1,501):
    data = {
        "worksTypes": 0,
        "bookType": 1,
        "subWorksTypes": 0,
        "totalWord": 0,
        "serialStatus": 1,
        "vip": 9,
        "pageNum": {i},
        "pageSize": 20,
        "categoryId": 0,
        "categoryPid": 0,
        "order": "weekOrder",
        "naodongFilter": 0
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
        "referer": 'https://www.zongheng.com/books?worksTypes=0&bookType=1&subWorksTypes=0&totalWord=0&serialStatus=1&vip=9'
    }
    url = 'https://www.zongheng.com/api2/catefine/storeSearch'
    response = requests.post(url, data=data, headers=headers)
    # print(response.json())
    # https://huayu.zongheng.com/showchapter/1336537.html 目录页
    items = response.json()['result']['bookList']
    for item in items:
        name = item['name']
        book_id = item['bookId']
        print(name, book_id)
        name_url = f'https://huayu.zongheng.com/showchapter/{book_id}.html'  # 进入对应小说的详情页
        # print(name_url)

        name_res = requests.get(name_url, headers=headers)
        items = name_res.text
        # print(items)
        # break

        tree = etree.HTML(items)
        items = tree.xpath('//ul[@class="chapter-list clearfix"]/li')
        for item in items:
            name1 = item.xpath('./a/text()')[0]  # 小说章节
            name2 = item.xpath('./a/@href')[0]  # 小说章节的url
            # print(name2)
            conect1 = requests.get(name2, headers=headers)
            conect2 = conect1.text
            tree = etree.HTML(conect2)
            title=tree.xpath('//div[@class="title_txtbox"]/text()')[0]
            print(title)
            items = tree.xpath('//div[@class="content"]/p/text()')
            print(items)
            # for item in items:
            #     name1 = item.xpath('./p/text()')[0]
            #     print(name1)
        time.sleep(1)